package net.roobook.cms.modules.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import net.roobook.cms.common.annotation.Log;
import net.roobook.cms.common.annotation.Log.OperatorType;
import net.roobook.cms.common.web.BaseController;
import net.roobook.cms.modules.admin.entity.Archives;
import net.roobook.cms.modules.admin.service.ArchivesService;
import net.roobook.cms.modules.admin.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

/**
 * @author cos
 * @email 327878610@qq.com
 * @link https://roobook.net
 * @Description 标签controller
 * @date 2023/9/26 15:42
 **/
@Controller
@RequestMapping("/admin/label")
public class LabelController extends BaseController {
    @Autowired
    private LabelService labelService;

    @Autowired
    private ArchivesService archivesService;

    @Log(operType = OperatorType.SELECT, module = "标签管理", content = "标签分组查询")
    @SaCheckPermission("user.add")
    @RequestMapping({"","/toIndex"})
    public String toIndex(Model model) {
        String[] classs = {"label-default","label-primary","label-success","label-info","label-inverse","label-warning","label-danger"};
        Map<String, List<Map<String,String>>> map = labelService.queryLabel();
        model.addAttribute("labels", map);
        model.addAttribute("classs", classs);
        return "admin/label/list";
    }

    @Log(operType = OperatorType.DELETE, module = "标签管理", content = "删除标签")
    @RequestMapping("/delete")
    public String delete(String tagName) {
        List<Archives> list = archivesService.queryListByTagName(tagName);
        if(list != null && list.size()>0) {
            for(int i=0;i<list.size();i++) {
                StringBuffer sb = new StringBuffer();
                Archives article = list.get(i);
                String tag = article.getTag();
                String[] tags = tag == null ? null : tag.split(",");
                if(tags != null && tags.length > 0) {
                    for(int j = 0;j < tags.length;j++) {
                        String temp = tags[j];
                        if(!tagName.equals(temp)) {
                            sb.append(temp + ",");
                        }
                    }
                    String newTag = sb.length() > 0 ? sb.substring(0, sb.length() - 1) : "";
                    article.setTag(newTag);
                    archivesService.update(article);
                }
            }
        }
        labelService.deleteByTagName(tagName);
        return "redirect:/admin/label/toIndex";
    }
}
